package com.cn.download.servlet;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import sun.misc.BASE64Encoder;

public class DownloadUtils {

	public static String encode(String fileName, HttpServletRequest request) {
		String agent = request.getHeader("User-Agent");
		if(agent.contains("Firefox")){
			BASE64Encoder base64Encoder = new BASE64Encoder();
			try {
				fileName = "=?utf-8?B?"
						+ base64Encoder.encode(fileName.getBytes("utf-8")) + "?=";
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			try {
				fileName = URLEncoder.encode(fileName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return fileName;
	}

}
